package com.surpassun.emall.repository;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.surpassun.emall.model.Role;
import com.surpassun.emall.util.Validator;

/**
 *
 * @author Denis
 */
@Repository
public class RoleRepository extends AbstractRepository<Role> {
	
	private final String QUERY_GET_ROLE_BY_NAME = "Role_GetRoleByName";

    public RoleRepository() {
        super(Role.class);
    }

	public Role getRole(String roleName) {
		Query query = super.getEntityManager().createNamedQuery(QUERY_GET_ROLE_BY_NAME);
    	query.setParameter("name", roleName);
    	query.setMaxResults(1);
    	@SuppressWarnings("unchecked")
		List<Role> roles = query.getResultList();
    	if (Validator.isNotEmpty(roles)) {
    		return roles.get(0);
    	} else {
    		return null;
    	}
	}
}